package com.ymatou.iapi.godmch.testcase;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.DeleteMarkupConfigBean;
import com.ymatou.iapi.godmch.parameter.SetMarkupConfigBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.DeleteMarkupConfigCall;
import com.ymatou.iapi.godmch.service.SetMarkupConfigCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.MarkupConfig;
import com.ymttest.database.sqlwapper.godmch.MarkupconfigWapper;

/**
 * 删除自动加价规则Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_DeleteMarkupConfig {
	private static DeleteMarkupConfigBean deletemarkupconfigBean;
	private static DeleteMarkupConfigCall deletemarkupconfigCall;
	private MqQueryCallService mqQueryCallService=new MqQueryCallService();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除自动加价规则Test");
	}

	@Before
	public void caseUp() {
		deletemarkupconfigBean = new DeleteMarkupConfigBean();
		deletemarkupconfigCall = new DeleteMarkupConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deletemarkupconfigCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_DeleteMarkupConfig_001() {
		Logger.start(true, "删除自动加价规则，成功");
		try {
//			// 创建渠道商
//			CreateCodeBean createAllMsgSellerMch = GodmchManageCallService.CreateAllMsgSellerMch();
//			// 创建渠道商邀请码
//			CreateCodeCall createCodeCall2 = GodmchManageCallService.createCodeCall(createAllMsgSellerMch);
//			// 创建代理商
//			Integer agentUserId = GodmchManageCallService.CreateSellerMch(
//					createCodeCall2.getJsonObject().getAsJsonObject("result").get("code").getAsString());
//			int rootUserId = createAllMsgSellerMch.getMchId();
//			
//			SetMarkupConfigBean bean = GodmchManageCallService.initSetMarketupBean(agentUserId, Arrays.asList(rootUserId));
//			SetMarkupConfigCall call = GodmchManageCallService.SetMarketupBeanCall(bean);
//			System.out.println(call.getJsonObject());
//			
//			deletemarkupconfigBean.setAgentId(agentUserId);
//			deletemarkupconfigBean.setSupplierId(rootUserId);
			
			//查询一条
			String creatTime=LocalDateTime.now().plusDays(-1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss "));
			Logger.comment("creatTime:"+creatTime);
			MarkupConfig deleteConfig=  new MarkupconfigWapper().selectByCreatTime (creatTime).get(0);
			deletemarkupconfigBean.setAgentId(deleteConfig.getAgentId());
			deletemarkupconfigBean.setSupplierId(deleteConfig.getSupplierId());
			
			deletemarkupconfigCall.setData(deletemarkupconfigBean);
			deletemarkupconfigCall.callService();
			
			Logger.verifyEquals("200", deletemarkupconfigCall.getString("status"), "验证code");
			
			MarkupConfig markupConfig = new MarkupconfigWapper().selectbyPrimaryKey(deletemarkupconfigBean.getAgentId(), deletemarkupconfigBean.getSupplierId()).get(0);
			Logger.verifyEquals(markupConfig.getConfigStatus(), -1, "已经删除");
			
			//验证发消息总线-删除
	        MessageListBean messageListBean=mqQueryCallService.initGodmchManage(String.valueOf(deletemarkupconfigBean.getAgentId()), "set_markupconfig");
	        List <Map> mq=mqQueryCallService.GetMqMessage(messageListBean);
	        if(mq.size()==0){
				Thread.sleep(5000);
				mq = MqQueryCallService.GetMqMessage(messageListBean);
			}
	        Logger.verifyEquals(deletemarkupconfigBean.getAgentId(),mq.get(0).get("agentId"), "验证消息总线代理Id AgentId");
	        Logger.verifyEquals(deletemarkupconfigBean.getSupplierId(),mq.get(0).get("supplierId"), "验证消息总线服务商 SupplierId");
	        Logger.verifyEquals(3,mq.get(0).get("operateType"), "验证消息总线operateType=3删除自动加价");
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_DeleteMarkupConfig_001_1() {
		Logger.start(true, "删除自动加价规则，代理商id与供货商id关系不存在- 成功");
		try {
			
			
			deletemarkupconfigBean.setAgentId(1111);
			deletemarkupconfigBean.setSupplierId(22222);
			deletemarkupconfigCall.setData(deletemarkupconfigBean);
			deletemarkupconfigCall.callService();
			Logger.verifyEquals("5000", deletemarkupconfigCall.getString("status"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_DeleteMarkupConfig_002() {
		Logger.start(true, "删除自动加价规则之后，不会自动上架商品。成功");
		try {
			// 创建渠道商
			CreateCodeBean createAllMsgSellerMch = GodmchManageCallService.CreateAllMsgSellerMch();
			// 创建渠道商邀请码
			CreateCodeCall createCodeCall2 = GodmchManageCallService.createCodeCall(createAllMsgSellerMch);
			// 创建代理商
			Integer agentUserId = GodmchManageCallService.CreateSellerMch(
					createCodeCall2.getJsonObject().getAsJsonObject("result").get("code").getAsString());
			int rootUserId = createAllMsgSellerMch.getMchId();
			
			SetMarkupConfigBean bean = GodmchManageCallService.initSetMarketupBean(agentUserId, Arrays.asList(rootUserId));
			SetMarkupConfigCall call = GodmchManageCallService.SetMarketupBeanCall(bean);
			System.out.println(call.getJsonObject());
			
			deletemarkupconfigBean.setAgentId(agentUserId);
			deletemarkupconfigBean.setSupplierId(rootUserId);
			deletemarkupconfigCall.setData(deletemarkupconfigBean);
			deletemarkupconfigCall.callService();
			Logger.verifyEquals("200", deletemarkupconfigCall.getString("status"), "验证code");
			
			MarkupConfig markupConfig = new MarkupconfigWapper().selectbyPrimaryKey(agentUserId, rootUserId).get(0);
			Logger.verifyEquals(markupConfig.getConfigStatus(), -1, "已经删除");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}